from pynput import keyboard
from pynput import mouse
import time

key_pressed_when_shift = False
is_pressed = False

left_key = 'caps_lock'
left_key_replace = 'fsdfds'
left_key_replace_pressed = False

right_key = 'page_down'
right_key_replace = 'ctrl_r'
right_key_replace_pressed = False

press_key = 'Key.f1'
press_key_replace = 'insert'
press_key_replace_pressed = False

is_left_key_pressed = False
import json
ss = '''
[["<96>", 96], ["<97>", 97], ["<98>", 98], ["<99>", 99], ["<100>", 100], ["<101>", 101], ["<102>", 102], ["<103>", 103], ["<104>", 104], ["<105>", 105], ["<110>", 110], ["'+'", 107], ["'-'", 109], ["'*'", 106], ["'/'", 111]]
'''
print(ss)

numL = json.loads(ss)
print(numL)

def decode(key):
    if not hasattr(key, 'vk') :
        return -1
    for i in range(len(numL)):
        if numL[i] == [str(key), key.vk]:
            return i
    return -1
        
sum = 0
def on_press(key):
    a = decode(key)
    print(a)
    global sum
    if a == -1:
        sum = 0
        return
    if a >= 0 and a <= 9:
        sum = sum * 10 + a
        return
    if a == 10:
        mouse.Controller().move(0, sum)
        sum = 0
        return
    if a == 11:
        mouse.Controller().move(sum, 0)
        sum = 0
        return
    if a == 12:
        mouse.Controller().move(sum, 0)
        sum = 0
        return
    if a == 13:
        mouse.Controller().move(0, -sum)
        sum = 0
        return
    if a == 14:
        mouse.Controller().move(-sum, 0)
        sum = 0
        return
        
        
    
    return
    global co
    
    kk = str(key)
    a = [kk, key.vk]
    numL.append(a)
    if len(numL) == 15:
        
        a = json.dumps(numL)
        print(a)
    if hasattr(key, 'vk') :
        print(key.vk, kk)
    
    return
    global key_pressed_when_shift, is_pressed, is_left_key_pressed
    global left_key_replace_pressed, right_key_replace_pressed, press_key_replace_pressed
    if left_key_replace in  str(key):
        left_key_replace_pressed = True
    if right_key_replace in  str(key):
        right_key_replace_pressed = True
    if press_key_replace in  str(key):
        press_key_replace_pressed = True                
    
    if 'ctrl' in str(key):        
        key_pressed_when_shift = False        
        
    if left_key  in str(key) and not left_key_replace_pressed and not is_left_key_pressed:
        is_left_key_pressed = True
        mouse.Controller().press(mouse.Button.left)
        #mouse.Controller().click(mouse.Button.left,1)                        
        return
    
    if right_key  in str(key) and not right_key_replace_pressed:
        mouse.Controller().click(mouse.Button.right,1)                        
        return
        
    if press_key == str(key) and not press_key_replace_pressed:    
        if not is_pressed:
            mouse.Controller().press(mouse.Button.left)
            print('p')
            is_pressed = True
        else:
            mouse.Controller().release(mouse.Button.left)
            is_pressed = False
        return    
        
def on_release(key):
    #print('rele', key)
    return
    global left_key_replace_pressed, right_key_replace_pressed, press_key_replace_pressed    
    global is_pressed, is_left_key_pressed
    
    if left_key_replace in  str(key):
        left_key_replace_pressed = False
    if right_key_replace in  str(key):
        right_key_replace_pressed = False
    if press_key_replace in  str(key):
        press_key_replace_pressed = False
    if left_key  in str(key) and not left_key_replace_pressed:
        is_left_key_pressed = False
        mouse.Controller().release(mouse.Button.left)
        #mouse.Controller().click(mouse.Button.left,1)                        
        return
        
    if 'ctrl' in str(key):    
        print(is_pressed, key_pressed_when_shift)
        if is_pressed and not key_pressed_when_shift:
            print('hit')
            mouse.Controller().release(mouse.Button.left)
            is_pressed = False            
   
listener = keyboard.Listener(
    on_release=on_release,
    on_press=on_press,
    
)
listener.start()
import time
while True:
    time.sleep(100)